Dar, PhD student in Physics, working in experimental quantum computing, Quantum Nanoelectronic Lab.
DarDahlen@berkeley.edu
150 LeConte - W 4-6pm
0) If you don't understand why it's working, you aren't done yet
1) Google is your friend
2) Someone, somewhere, has already tried to do what I'm trying to do
3) Guess and check works suprisingly well
4) RTFM (Read the Manual)
0) You can download this notebook from github.com/dahlend/Physics77Fall17
1) Install Python 3.6 (via Anaconda)
2) Get Jupyter Running
3) Write a simple program
4) Bonus Problems at the bottom if you get bored
Anaconda is a pre-packaged python that includes effectively all the tools we will use in this course. In addition, it allows us to install librarys we are missing in a very simple manner.
Step 1 of today will be downloading and installing Anaconda.
https://www.continuum.io/downloads
Download and install Python 3.6 appropriate for your OS.
In [1]:
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
# Defines an x vector, going from 0 to 2 pi in 20 steps
x = np.linspace(start=0, stop=2*np.pi, num=20)
plt.plot(x, np.sin(x), label='Sin') # Plot sin
plt.plot(x, np.cos(x), 'o-', label='Cos') # Plot cos
plt.legend() # add a legend to the plot
print("We made this vector x:")
print(x) # print the vector we made
Now that you have anaconda installed, we will open a jupyter notebook.
Then type in:
jupyter notebook
Days where we don't have assigned homework will often include a bonus problem. These problems will often be at or beyond the limits of what have currently been covered in class. Don't feel bad if you currently have NO idea how to do it, by the end of the semester you should have the tools needed to attempt them!
Plot an exponential decay of the form:
$y = e^{-t}$
With t going from 0 to 5.
Label the x-axis and y-axis appropriately, and put a title on the plot.
You might have to do some googling for this, the plotting tool we are using is called matplotlib.
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
In [2]:
# Code comments start with a # symbol, which means python ignores the line after it
# Many tools you need are in things called Libraries/Packages/Modules,
# these libraries are often built for free by hobbyists
# Python's primary plotting library is called matplotlib
# Librarys are just Folders in your computer stored in a specific location
# you can get to sub-folders with a . like so:
# You will see this line of code very very often:
import matplotlib.pyplot as plt
# this essentially imports a folder called pyplot from the folder matplotlib
# but since the name is kind of long, we can say import it but call it something
# else, in this case we are calling it plt
# another tool we will use very very often is Numpy
# numpy is short for numerical python
import numpy as np
# as you google more and more you will see it is common practice to call
# these libraries by thier short names, np and plt
# This next line is not actually python, it is Jupyter
%matplotlib inline
# anytime you see a line start with % it is telling
# jupyter to do something, in this case we are telling
# jupyter that we are using matplotlib library, and we want plots "inline"
# which means it plots it in the jupyter notebook.
# Defines an x vector of 20 number, going from 0 to 2 pi
x = np.linspace(start=0, stop=2*np.pi, num=20)
What is going on here?
We are calling np.linspace, which is a function from the numpy library.
Python functions are conceptually similar to a math function, where we pass it variables,
start=0, stop=2*np.pi, num=20
and the function does something and has a result
In math it would be something like f(x, y, z), but in python:
np.linspace(start=0, stop=2*np.pi, num=20)
But now I want to keep track of the result of that function. So I assign it to a Variable, in this case called 'x'
x = np.linspace(start=0, stop=2*np.pi, num=20)
from now on, any time I use x it is the result of that function (unless I redefine x)
How did I know what variables a function accepts and what it does?
Lets take a look at documentation: np.linspace()
or:
In [3]:
help(np.linspace) # Very helpful!
In [4]:
plt.plot(x, np.sin(x), label='Sin') # Plot sin
plt.plot(x, np.cos(x), 'o-', label='Cos') # Plot cos
plt.legend() # add a legend to the plot
print("We made this vector x:")
print(x) # print the vector we made
In [5]:
help(plt.plot) # This one is super long
In [ ]: